package com.xyz.controller;

import com.xyz.aop.ObjectParam;
import com.xyz.aop.Param;
import com.xyz.dto.SelectPageTaskDto;
import com.xyz.dto.SelectPageTempDto;
import com.xyz.dto.TemplatePlanDto;
import com.xyz.dto.UpkeepTemplateTaskDto;
import com.xyz.service.TemplatePlanService;
import com.xyz.util.dto.DataResult;
import com.xyz.util.dto.LoginInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("template")
public class TemplateController {

    private static final Logger logger = LoggerFactory.getLogger(TemplateController.class);

    @Autowired
    private TemplatePlanService templatePlanService;


    /*
    * 查询模版列表
    * */
    @PostMapping("/getTemplatePlanAll")
    public DataResult getTemplatePlanAll(
            LoginInfo loginInfo,
            @ObjectParam SelectPageTempDto selectPageTempDto
    ) {
        try {
            return templatePlanService.getTemplatePlanAll(loginInfo,selectPageTempDto);
        } catch (Exception e) {
            logger.error("getTemplatePlanAll", e);
            return DataResult.build9500();
        }
    }



    /*
     * 根据ID查询详情
     * */
    @PostMapping("/getTemplatePlanById")
    public DataResult getTemplatePlanById(
            LoginInfo loginInfo,
            @Param(type = Param.ParamType.POSITIVE_INTEGER) String id
    ) {
        try {
            return templatePlanService.getTemplatePlanById(loginInfo,id);
        } catch (Exception e) {
            logger.error("getTemplatePlanById", e);
            return DataResult.build9500();
        }
    }

    /*
     * 根据ID查询工作项详情
     * */
    @PostMapping("/getTemplatePlanTaskById")
    public DataResult getTemplatePlanTaskById(
            LoginInfo loginInfo,
            @Param(type = Param.ParamType.POSITIVE_INTEGER) String id
    ) {
        try {
            return templatePlanService.getTemplatePlanTaskById(loginInfo,id);
        } catch (Exception e) {
            logger.error("getTemplatePlanTaskById", e);
            return DataResult.build9500();
        }
    }

    /*
     * 根据ID查询零件详情
     * */
    @PostMapping("/getTemplatePartById")
    public DataResult getTemplatePartById(
            LoginInfo loginInfo,
            @Param(type = Param.ParamType.POSITIVE_INTEGER) String id
    ) {
        try {
            return templatePlanService.getTemplatePartById(loginInfo,id);
        } catch (Exception e) {
            logger.error("getTemplatePartById", e);
            return DataResult.build9500();
        }
    }


    /*
     * 查询模版工作项列表
     * */
    @PostMapping("/getTemplatePlanTask")
    public DataResult getTemplatePlanTask(
            LoginInfo loginInfo,
            @ObjectParam SelectPageTaskDto selectPageTaskDto

    ) {
        try {
            return templatePlanService.getTemplatePlanTask(loginInfo, selectPageTaskDto);
        } catch (Exception e) {
            logger.error("getTemplatePlanTask", e);
            return DataResult.build9500();
        }
    }

    /*
     * 保存模版计划
     * */
    @PostMapping("/insertTemplatePlan")
    public DataResult insertTemplatePlan(LoginInfo loginInfo,@ObjectParam TemplatePlanDto templatePlanDto) {
        try {
            return templatePlanService.insertTemplatePlan(loginInfo,templatePlanDto);
        } catch (Exception e) {
            logger.error("insertTemplatePlan", e);
            return DataResult.build9500();
        }
    }

    /*
     * 编辑模版计划
     * */
    @PostMapping("/updateTemplatePlan")
    public DataResult updateTemplatePlan(
            LoginInfo loginInfo,
            @ObjectParam TemplatePlanDto templatePlanDto
    ) {
        try {
            return templatePlanService.updateTemplatePlan(loginInfo,templatePlanDto);
        } catch (Exception e) {
            logger.error("updateTemplatePlan", e);
            return DataResult.build9500();
        }
    }

    /*
     * 编辑模版计划任务项
     * */
    @PostMapping("/updateTemplateTask")
    public DataResult updateTemplateTask(
            LoginInfo loginInfo,
            @Param(type = Param.ParamType.POSITIVE_INTEGER) String id,
            @Param(isRequired = false) String taskIds
    ) {
        try {
            return templatePlanService.updateTemplateTask(loginInfo,id,taskIds);
        } catch (Exception e) {
            logger.error("updateTemplateState", e);
            return DataResult.build9500();
        }
    }

    /*
     * 编辑模版计划零件
     * */
    @PostMapping("/updateTemplatePart")
    public DataResult updateTemplatePart(
            LoginInfo loginInfo,
            @Param(type = Param.ParamType.POSITIVE_INTEGER) String id,
            @Param(isRequired = false) String partList
    ) {
        try {
            return templatePlanService.updateTemplatePart(loginInfo,id,partList);
        } catch (Exception e) {
            logger.error("updateTemplatePart", e);
            return DataResult.build9500();
        }
    }

    /*
     * 修改模版状态
     * */
    @PostMapping("/updateTemplateState")
    public DataResult updateTemplateState(
            LoginInfo loginInfo,
            @Param(type = Param.ParamType.POSITIVE_INTEGER) String id,
            @Param(type = Param.ParamType.POSITIVE_INTEGER) String state
    ) {
        try {
            return templatePlanService.updateTemplateState(loginInfo,id,state);
        } catch (Exception e) {
            logger.error("updateTemplateState", e);
            return DataResult.build9500();
        }
    }


    /*
     * 绑定模版计划
     * */
    @PostMapping("/templateBindingEquipment")
    public DataResult templateBindingEquipment(
            LoginInfo loginInfo,
            @Param(type = Param.ParamType.POSITIVE_INTEGER) String emId,
            @Param(isRequired = false) String mainTemplateIds,
            @Param(isRequired = false) String inspTemplateIds
    ) {
        try {
            return templatePlanService.templateBindingEquipment(loginInfo,emId,mainTemplateIds,inspTemplateIds);
        } catch (Exception e) {
            logger.error("templateBindingEquipment", e);
            return DataResult.build9500();
        }
    }

    /*
     * 绑定巡检模版计划
     * */
    @PostMapping("/templateBindingEquipmentInsp")
    public DataResult templateBindingEquipmentInsp(
            LoginInfo loginInfo,
            @Param(type = Param.ParamType.POSITIVE_INTEGER) String emId,
            @Param(isRequired = false) String mainTemplateIds,
            @Param(isRequired = false) String inspTemplateList
    ) {
        try {
            return templatePlanService.templateBindingEquipmentInsp(loginInfo,emId,mainTemplateIds,inspTemplateList);
        } catch (Exception e) {
            logger.error("templateBindingEquipmentInsp", e);
            return DataResult.build9500();
        }
    }

    /*
     * 查询绑定模版计划
     * */
    @PostMapping("/getTemplateBindingEquipment")
    public DataResult getTemplateBindingEquipment(
            LoginInfo loginInfo,
            @Param(type = Param.ParamType.POSITIVE_INTEGER) String emId
    ) {
        try {
            return templatePlanService.getTemplateBindingEquipment(loginInfo,emId);
        } catch (Exception e) {
            logger.error("getTemplateBindingEquipment", e);
            return DataResult.build9500();
        }
    }


    /*
     * 查询维保计划配置
     * */
    @PostMapping("/selectUpkeepTemplateTask")
    public DataResult selectUpkeepTemplateTask(
            LoginInfo loginInfo,
            @Param(type = Param.ParamType.POSITIVE_INTEGER) String emId
    ) {
        try {
            return templatePlanService.selectUpkeepTemplateTask(loginInfo,emId);
        } catch (Exception e) {
            logger.error("selectUpkeepTemplateTask", e);
            return DataResult.build9500();
        }
    }

    /*
     * 保存维保计划配置
     * */
    @PostMapping("/insertUpkeepTemplateTask")
    public DataResult insertUpkeepTemplateTask(
            LoginInfo li,
            @ObjectParam UpkeepTemplateTaskDto UpkeepTemplateTaskDto
    ) {
        try {
            return templatePlanService.insertUpkeepTemplateTask(li, UpkeepTemplateTaskDto);
        } catch (Exception e) {
            logger.error("insertUpkeepTemplateTask", e);
            return DataResult.build9500();
        }
    }


    /*
     * 根据ID查询详情
     * */
    @PostMapping("/getUpkeepTemplateTaskById")
    public DataResult getUpkeepTemplateTaskById(
            LoginInfo li,
            @Param(type = Param.ParamType.POSITIVE_INTEGER) String id
    ) {
        try {
            return templatePlanService.getUpkeepTemplateTaskById(li, Long.parseLong(id));
        } catch (Exception e) {
            logger.error("getUpkeepTemplateTaskById", e);
            return DataResult.build9500();
        }
    }
    /*
     * 编辑维保计划配置
     * */
    @PostMapping("/updateUpkeepTemplateTask")
    public DataResult updateUpkeepTemplateTask(
            LoginInfo li,
            @ObjectParam UpkeepTemplateTaskDto UpkeepTemplateTaskDto
    ) {
        try {
            return templatePlanService.updateUpkeepTemplateTask(li, UpkeepTemplateTaskDto);
        } catch (Exception e) {
            logger.error("updateUpkeepTemplateTask", e);
            return DataResult.build9500();
        }
    }

    /*
     * 删除维保计划配置
     * */
    @PostMapping("/deleteUpkeepTemplateTask")
    public DataResult deleteUpkeepTemplateTask(
            LoginInfo li,
            @Param(type = Param.ParamType.POSITIVE_INTEGER) String id
    ) {
        try {
            return templatePlanService.deleteUpkeepTemplateTask(li, Long.parseLong(id));
        } catch (Exception e) {
            logger.error("deleteUpkeepTemplateTask", e);
            return DataResult.build9500();
        }
    }

    /*
     * 修改维保计划配置状态
     * state = 2，启用；3，停用；4，作废
     * */
    @PostMapping("/updateUpkeepTemplateTaskState")
    public DataResult updateUpkeepTemplateTaskState(
            LoginInfo li,
            @Param(type = Param.ParamType.POSITIVE_INTEGER) String id,
            @Param(type = Param.ParamType.QUALIFIER, rule = "2,3,4") String state
    ) {
        try {
            return templatePlanService.updateUpkeepTemplateTaskState(li, Long.parseLong(id),Integer.parseInt(state));
        } catch (Exception e) {
            logger.error("updateUpkeepTemplateTaskState", e);
            return DataResult.build9500();
        }
    }

}
